#re  表达式 实战  爬取豆瓣排行榜
#1.获取页面源代码  get /post  服务器渲染 还是 客户端渲染
#2. 利用 re表达式  获取所需内容
import re
import requests
import csv
#csv 特点  a,b,c,d  以逗号为分割  为了数据分析
url="https://movie.douban.com/top250"
#源代码
dic={
    'Cookie':
'bid=R2795R4pNwk; douban-fav-remind=1; ll="118237"; dbcl2="284516089:enmP5uizWQw"; push_noty_num=0; push_doumail_num=0; __utmv=30149280.28451; _pk_id.100001.4cf6=21e383f6b0b5d825.1730651222.; __yadk_uid=3HBpOU5UcR0epXbLDCfPZJLRqgtrbUHo; _vwo_uuid_v2=D9EA9C8DB05538004C99D326E4F539398|3f4180df83b1ac32874b7b69763e736e; __utmz=30149280.1731047063.7.2.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; ck=btlP; __utma=30149280.1054154903.1663398773.1731047063.1731099042.8; __utmc=30149280; __utmt=1; _pk_ref.100001.4cf6=%5B%22%22%2C%22%22%2C1731099053%2C%22https%3A%2F%2Fwww.douban.com%2F%22%5D; _pk_ses.100001.4cf6=1; __utma=223695111.640495425.1730651222.1730651222.1731099053.2; __utmb=223695111.0.10.1731099053; __utmc=223695111; __utmz=223695111.1731099053.2.2.utmcsr=douban.com|utmccn=(referral)|utmcmd=referral|utmcct=/; frodotk_db="ed2c8dbf5dd035adc785a93e1970a107"; __utmb=30149280.12.9.1731099269519'
,"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36 Edg/126.0.0.0"
}
#用户 ua 伪装 防止反爬  cookie  反爬（进一步）

resp = requests.get(url, headers=dic)
print(resp.text)#检查是否获取到页面源代码
page_content=resp.text
resp.close()
#获取所要数据步骤   1.  找到合适的前期定位规则(建议上上级别标签)  2. 缩小范围 寻找规律 数据所在位置缩小 3. group精确获取
obj = re.compile(r'<li>.*?<div class=\"item\">.*?<span class=\"title\">(?P<name>.*?)</span>'
                 r'.*?<p class="">.*?<br>(?P<year>.*?)&nbsp'
                 r'.*?<span class="rating_num" property="v:average">(?P<score>.*?)</span>.*?'
                 r'<span>(?P<num>.*?)人评价</span>',re.S)

result = obj.finditer(page_content)

#csv文件用于数据分析  pandas
f = open("data.csv",mode="w",encoding="utf-8") #准备csv文件
csvwriter = csv.writer(f) #准备一个csv Writer
for it in result:#把数据处理成字典
    #print(it.group("name"))
    #print(it.group("score"))
    #print(it.group("num"))
    #print(it.group("year").strip())
    dic = it.groupdict()
    dic['year'] = dic['year'].strip()
    csvwriter.writerow(dic.values())
f.close()
print("over")








